<template>
  <div class="mh"></div>
  <div class="box" ref="box" id="xx">
    <template v-for="item in list" :key="item.index">
      <div class="user-item" v-if="item.type === 2">
        <div class="user-content">
          {{ item.content }}
        </div>
        <div class="user-avatar">
          <img :src="appStore.user.avatar" v-if="appStore.user?.avatar" />
          <img src="@/assets/logo.png" v-else />
        </div>
      </div>
      <div class="ai-item" v-else>
        <div class="ai-avatar">
          <img src="@/assets/logo.png" />
        </div>
        <div class="ai-content" v-html="item.content" v-highlight></div>
      </div>
    </template>
    <div class="w" v-if="waitServer">--</div>
    <div class="search-box">
      <input
        class="input"
        placeholder="io something..."
        v-model="msg"
        @keyup.enter="sendMsg"
      />
      <button class="btn" @click="sendMsg" :class="{ 'btn-act': msg && !waitServer }">
        >
      </button>
    </div>
  </div>
  <div class="f"></div>
  <div class="back" @click="toBack">X</div>
</template>

<script setup lang="ts">
import { useRouter } from "vue-router";
import { pageBySessionId, PageBySessionItemType } from "@/api/ai";
import { onMounted, ref } from "vue";
import { useAppStore } from "@/store/modules/app";
import { message } from "ant-design-vue";

// import hljs from "highlight.js/lib/common";
import "highlight.js/styles/github-dark-dimmed.css"; //导入代码高亮样式

const appStore = useAppStore();

const route = useRouter();

const waitServer = ref(false);

const toBack = () => {
  route.go(-1);
};

const param = ref({
  pageSize: 100000,
  pageNum: 1,
  sessionId: route.currentRoute.value.query.sessionId as string,
});

const title = route.currentRoute.value.query.title as string;

// const title =
let ws: WebSocket;
const connect = () => {
  ws = new WebSocket(
    `${import.meta.env.VITE_WS_URL}/ai/ws/chat/${param.value.sessionId}`
  );

  ws.onopen = function () {
    if (title) {
      ws.send(title);
      list.value.push({ content: title, type: 2 });
      waitServer.value = true;
      toButtom();
    }
  };

  ws.onmessage = function (evt) {
    list.value.push({ content: evt.data, type: 1 });
    waitServer.value = false;
    toButtom();
  };

  ws.onclose = function () {
    message.error("连接已关闭...");
    route.push("/");
  };
};

const list = ref<PageBySessionItemType[]>([]);

onMounted(() => {
  pageBySessionId(param.value).then((res) => {
    list.value = res.records.reverse();
    connect();
    toButtom();
  });
});

const msg = ref("");
const sendMsg = () => {
  if (!msg.value || waitServer.value) {
    return false;
  }
  ws.send(msg.value);
  list.value.push({ content: msg.value, type: 2 });
  msg.value = "";
  waitServer.value = true;
  toButtom();
};

const toButtom = () => {
  setTimeout(() => {
    window.scrollTo(0, 5000);
  }, 200);
};
</script>

<style lang="less" scoped>
body {
  background-color: #efede6;
}
.box {
  margin: 0 auto;
  width: 800px;
  height: auto;
  // padding: 50px 0 0 0;
  padding-top: 30px;
  padding-bottom: 140px;
  // overflow-y: scroll;
}

.mh {
  z-index: 99;
  width: 100%;
  height: 40px;
  background: #efede6;
  opacity: 0.8;
  position: fixed;
  top: 0;
  // background: linear-gradient(to right, #0c0c0c, #efede6);
}

.f {
  width: 100%;
  height: 130px;
  position: fixed;
  background: #efede6;
  bottom: 0;
  z-index: 20;
  margin: 0;
}

.back {
  position: fixed;
  top: 20px;
  left: 20px;
  z-index: 9999;
  cursor: default;
  &:hover {
    font-size: 24px;
  }
}

.user-item,
.ai-item {
  margin-top: 20px;
}

.user-item {
  text-align: right;
  margin-top: 40px;
}

.ai-avatar {
  float: left;
  width: 40px;
  img {
    width: 40px;
    height: 40px;
    border-radius: 50%;
  }
}
.ai-content {
  width: 100%;
  background: #fff;
  border: none;
  border-radius: 8px;
  padding: 10px;
  margin-left: 50px;
  width: 660px;
}

.user-avatar {
  display: inline-block;
  width: 40px;
  img {
    width: 40px;
    height: 40px;
    border-radius: 50%;
  }
}
.user-content {
  background: #dfd6c8;
  border: none;
  border-radius: 8px;
  padding: 10px;
  margin-right: 10px;
  /* width: 750px; */
  // text-align: right;
  // float: right;
  max-width: 660px;
  word-wrap: break-word;
  color: rgb(28 25 23 / var(--tw-text-opacity));
  display: inline-block;
  text-align: left;
  position: relative;
  top: -14px;
}

.search-box {
  position: fixed;
  bottom: 50px;
  // margin: 0 auto;
  // width: 800px;
  z-index: 30;
}

.input {
  width: 750px;
  height: 50px;
  border-radius: 20px;
  border: 1px solid #ccc;
  padding-left: 50px;
  font-size: 18px;
}
.btn {
  position: relative;
  right: 100px;

  border: 1xp solid #ccc;
  padding: 6px 20px 6px 20px;
  border-radius: 12px;
  border: none;
  z-index: 20;
  &:hover {
    background: #5645a1;
    color: #fff;
  }
}
// div.code {
//   white-space: pre;
// }

.btn-act {
  background: #5645a1;
  color: #fff;
}

.w {
  width: 40px;
  height: 40px;
  background: #fff;
  border-radius: 50%;
  line-height: 40px;
  text-align: center;
  animation: rotate 2s linear infinite;
  margin-top: 20px;
}

@keyframes rotate {
  0% {
    transform: rotate(0);
  }
  100% {
    transform: rotate(360deg);
  }
}
</style>
